﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;

namespace Nivdal
{
    public partial class Live : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.Live_PageTitle;
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"]; 
            
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                Response.Redirect("~/Default.aspx");

            else
            {   
                if (ou.TeamId == "Null")
                    Response.Redirect("~/GeneralMessage.aspx?MessageString=M74ggdizz5wfeh");

                UserIdHiddenField.Value = ou.UserId;

                Title = Resources.Resource.Live_LiveMatchesTitle + " - nivdal.com!";

                if (ou.IsPremium)
                    IsPremiumHiddenField.Value = "true";
                else
                    IsPremiumHiddenField.Value = "false";
            }
            
        }

        protected bool DirLTR()
        {
            ContentDirection dir = (Master.Master.FindControl("DirectionPanel") as Panel).Direction;

            if (dir == ContentDirection.LeftToRight)
                return true;
            return false;

        }


        protected void LiveItemCommandHandler(object sender, ListViewCommandEventArgs e)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];
            if (e.CommandName == "Remove")
            {
                // remove match from live
                string Answer = GND.ReadNivdalStringData("LiveId", "Nivdal_LiveDeleteMatchFromLive", e.CommandArgument);
                LiveMatchesListView.DataBind();
            }
            else if (e.CommandName == "Bookmark")
            {
                // bookmark the match
                string Answer = GND.ReadNivdalStringData("UserId", "BookmarkedMatch", "Nivdal_BookmarkMatchSelect", ou.UserId, e.CommandArgument);
                Response.Redirect("Bookmarks.aspx?ConfirmNote=" + Answer);
            }

        }

        protected string EventTypeImage(object EventType)
        {
            string IconName = "info.png";
            switch (EventType.ToString())
            {
                case "cornercard45x50.gif":
                    IconName = "Corner.png";
                    break;
                case "gol45x50.gif":
                    IconName = "goal.png";
                    break;
                case "info45x50.gif":
                    IconName = "info.png";
                    break;
                case "medic45x50.gif":
                    IconName = "medic.png";
                    break;
                case "nogol45x50.png":
                    IconName = "nogoal.png";
                    break;
                case "replace45x50.gif":
                    IconName = "replace.png";
                    break;
                case "redcard45x50.gif":
                    IconName = "red.png";
                    break;
                case "yellowcard45x50.gif":
                    IconName = "yellow.png";
                    break;
                case "wissle45x50.gif":
                    IconName = "whistle.png";
                    break;
                default:
                    IconName = "pencil.png";
                    break;
            }

            return ("~/Images/MatchIcons/" + IconName);
        }

        protected string MinuteSting(object minute)
        {
            return (minute.ToString() + "` ");
        }

        protected void RefreshView(object sender, EventArgs e)
        {
            LiveUpdatepanel.DataBind();
        }

        protected bool RegularBackgroundVisible(object HomeScored, object AwayScored)
        {
            return (!(bool)HomeScored && !(bool)AwayScored);
        }

        protected bool HomeBackgroundVisible(object HomeScored, object AwayScored)
        {
            ContentDirection dir = (Master.Master.FindControl("DirectionPanel") as Panel).Direction;

            if (dir == ContentDirection.RightToLeft)
                return (!(bool)HomeScored && (bool)AwayScored);
            return ((bool)HomeScored && !(bool)AwayScored);
        }

        protected bool AwayBackgroundVisible(object HomeScored, object AwayScored)
        {
            ContentDirection dir = (Master.Master.FindControl("DirectionPanel") as Panel).Direction;

            if (dir == ContentDirection.RightToLeft)
                return ((bool)HomeScored && !(bool)AwayScored);
            return (!(bool)HomeScored && (bool)AwayScored);
        }
        protected bool BothBackgroundVisible(object HomeScored, object AwayScored)
        {
            return ((bool)HomeScored && (bool)AwayScored);
        }

        protected bool UserPremium()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            return ou.IsPremium;
        }

        protected void RemoveAll(object sender, EventArgs e)
        {
            string LiveId;
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            foreach (ListViewItem i in LiveMatchesListView.Items)
            {
                LiveId = (i.FindControl("LiveIdHiddenField") as HiddenField).Value;
                GND.ReadNivdalStringData("LiveId", "Nivdal_LiveDeleteMatchFromLive", LiveId);                
            }
            LiveMatchesListView.DataBind();
        }

        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }
    }
}